package com.monitor.app_monitor.service.quartz.task;

import com.monitor.app_monitor.entity.TaskLogAnnotation;
import io.micrometer.core.instrument.ImmutableTag;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.monitor.app_monitor.service.quartz.utils.logUtils;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class DateInfo implements Job {
    @Resource
    @Qualifier("dataKey")
    private Map<String, AtomicInteger> dataKeyMap;

    @Resource
    @Qualifier("dataKey2")
    private Map<String, AtomicInteger> dataKey2Map;

    @Autowired
    MeterRegistry registry;

    @TaskLogAnnotation
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        /**
         * 设置通用的七天时间格式
         */

        dataKeyMap.clear();
        dataKey2Map.clear();

        for (int i = 0; i < 7; i++) {
            Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
            ca.setTime(new Date());//月份是从0开始的，所以11表示12月
            ca.add(Calendar.DATE, -i); //月份减1
            Date lastMonth = ca.getTime(); //结果
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            String dataKey = sf.format(lastMonth);


            List<Tag> tags = new ArrayList<>();
            tags.add(new ImmutableTag("dataKey",dataKey));

            AtomicInteger list = registry.gauge("DataInfo", tags, new AtomicInteger(0));
            dataKeyMap.put(dataKey,list);
        }

        for (int i = 0; i < 7; i++) {
            Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
            ca.setTime(new Date());//月份是从0开始的，所以11表示12月
            ca.add(Calendar.DATE, -i); //月份减1
            Date lastMonth = ca.getTime(); //结果
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
            String dataKey = sf.format(lastMonth);


            List<Tag> tags = new ArrayList<>();
            tags.add(new ImmutableTag("dataKey",dataKey));

            AtomicInteger list = registry.gauge("DataInfo2", tags, new AtomicInteger(0));
            dataKey2Map.put(dataKey,list);
        }

    }
}
